package com.lp.common.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RedisConfig {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    @Value("${redis1.ip}")
    private String host;

    @Value("${redis1.port}")
    private int port;

    @Value("${redis1.timeout}")
    private int timeout;

    @Value("${redis.pool.maxIdle}")
    private int maxIdle;

    @Value("${redis.pool.maxWait}")
    private long maxWaitMillis;

    @Value("${redis1.pass}")
    private String password;

    @Bean
    public ShardedJedisPool shareRedisPoolFactory() throws Exception {
        log.info("JedisPool注入成功！！");
        log.info("redis地址：" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
        //jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);
        List<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>();
        JedisShardInfo jedisPool = new JedisShardInfo(host, port, timeout);
        jedisPool.setPassword(password);
        jdsInfoList.add(jedisPool);
        ShardedJedisPool shardedJedisPool =new ShardedJedisPool(jedisPoolConfig,jdsInfoList);
        return shardedJedisPool;
    }
}


